package com.gc.web.controller.business;

import java.util.List;
import java.util.Arrays;
import com.gc.common.base.Result;
import com.gc.common.support.Convert;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.gc.common.annotation.Log;
import com.gc.common.enums.BusinessType;
import com.gc.business.domain.UpkeepProject;
import com.gc.business.service.IUpkeepProjectService;
import com.gc.framework.web.base.BaseController;
import com.gc.common.utils.poi.ExcelUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * 电梯保养项目 控制层
 * 
 * @author hruiyan
 * @date 2020-11-18
 */
@Slf4j
@Controller
@RequestMapping("/business/upkeepProject")
public class UpkeepProjectController extends BaseController {
    private String prefix = "business/upkeepProject";
	
	private final IUpkeepProjectService upkeepProjectService;

	@Autowired
	public UpkeepProjectController(IUpkeepProjectService upkeepProjectService) {
		this.upkeepProjectService = upkeepProjectService;
	}

	/**
	*  列表页
	*/
	@RequiresPermissions("business:upkeepProject:view")
	@GetMapping
	public String upkeepProject(){
	    return prefix + "/upkeepProject";
	}

	/**
	 * 新增页
	 */
	@GetMapping("/add")
	public String add(){
		return prefix + "/add";
	}

	/**
	 * 编辑页
	 */
	@GetMapping("/edit/{projectId}")
	public String edit(@PathVariable("projectId") Long projectId, ModelMap mmap){
		UpkeepProject upkeepProject = upkeepProjectService.getById(projectId);
		mmap.put("upkeepProject", upkeepProject);
		return prefix + "/edit";
	}
	
	/**
	 * 查询电梯保养项目列表
	 */
	@RequiresPermissions("business:upkeepProject:list")
	@PostMapping("/list")
	@ResponseBody
	public Result list(UpkeepProject upkeepProject){
		startPage();
        List<UpkeepProject> list = upkeepProjectService.list(upkeepProject);
		return Result.success(getDataTable(list));
	}

	/**
	 * 新增保存电梯保养项目
	 */
	@RequiresPermissions("business:upkeepProject:add")
	@Log(title = "电梯保养项目", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(UpkeepProject upkeepProject){
		upkeepProjectService.save(upkeepProject);
		return Result.success();
	}

	/**
	 * 修改保存电梯保养项目
	 */
	@RequiresPermissions("business:upkeepProject:edit")
	@Log(title = "电梯保养项目", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(UpkeepProject upkeepProject){
		upkeepProjectService.updateById(upkeepProject);
		return Result.success();
	}
	
	/**
	 * 删除电梯保养项目
	 */
	@RequiresPermissions("business:upkeepProject:remove")
	@Log(title = "电梯保养项目", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public Result remove(String ids){
		Long[] longIds = Convert.toLongArray(ids);
		upkeepProjectService.removeByIds(Arrays.asList(longIds));
		return Result.success();
	}

	/**
	 * 导出电梯保养项目列表
	 */
	@RequiresPermissions("business:upkeepProject:export")
	@PostMapping("/export")
	@ResponseBody
	public Result export(UpkeepProject upkeepProject){
		List<UpkeepProject> list = upkeepProjectService.list(upkeepProject);
		ExcelUtil<UpkeepProject> util = new ExcelUtil<>(UpkeepProject.class);
		util.exportExcel(list, "upkeepProject");
		return Result.success();
	}
}
